const customerModel = require('../models/customer');
const {checkNameDup,userRegist} = require('../services/user')


async function main(ctx){
    console.log(123)

    //1.返回视图views
    await ctx.render('index')
}

async function checkName(ctx){
    const {name} = ctx.request.query;

    //从数据库获取是否重名

    // controller 调用 service(服务)
    const result = await checkNameDup(name)
    ctx.response.body = {status:result}
}



async function checkZzName(username){
    const reg = /^[\u4e00-\u9fa5a-zA-Z0-9_-]{1,5}$/
    return reg.test(username)
  }


async function regist(ctx){
    const {username,password} = ctx.request.body;
    //确保用户名不重名
    const result = await checkNameDup(username)
    const res = await checkZzName(username)
    let data = {status:true}
    if(res){
        if(result){
            data.status = false;
            data.message = '用户名重名' 
        }else{
            //用户才可以注册
            await userRegist({username,password})
        }
    }else{
        data.status =false;
        data.message = '用户名不符合要求'
    }

    ctx.response.body = data
}




//这是从routes 拆出来的东西 就module.exports什么东西 
//因为就算拆出来 也还是要作为模板（module）被引用 
module.exports = {
    main,
    checkName,
    regist
}